﻿/*****************************************************************
* Project: HFW Framework
*****************************************************************
* Developer: Hdhoan
* Date created: 11/2010
* Modified:
******************************************************************/
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace HFW.DataProvider
{
    public class Connection : IDisposable
    {
        private string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"].ToString();


        private string Provider = ConfigurationSettings.AppSettings["Provider"].ToString();
       

        public Connection()
        {
        }
        public void Dispose()
        {
        }

        #region No Parameter

        public DataSet RunSQLQuery(string strCmd, CommandType cmdType)
        {
            DataSet ds = new DataSet();
            if (Provider == "mssql")
            {
                ds = SqlHelper.ExecuteDataset(ConnectionString, cmdType, strCmd);
            }
            if (Provider == "mysql")
            {
                ds = OleDbHelper.ExecuteDataset(ConnectionString, cmdType, strCmd);
            }
            if (Provider == "oracle")
            {
                ds = OracleHelper.ExecuteDataset(ConnectionString, cmdType, strCmd);
            }
            return ds;
        }

        public DataSet RunSQLQuery(string strCmd, CommandType cmdType, string strConnection)
        {
            DataSet ds = new DataSet();
            if (Provider == "mssql")
            {
                ds = SqlHelper.ExecuteDataset(strConnection, cmdType, strCmd);
            }
            if (Provider == "mysql")
            {
                ds = OleDbHelper.ExecuteDataset(strConnection, cmdType, strCmd);
            }
            if (Provider == "oracle")
            {
                ds = OracleHelper.ExecuteDataset(strConnection, cmdType, strCmd);
            }
            return ds;
        }

        public int RunSQLNonQuery(string strCmd, CommandType cmdType)
        {
            int i = 0;
            if (Provider == "mssql")
            {
                i = SqlHelper.ExecuteNonQuery(ConnectionString, cmdType, strCmd);
            }
            if (Provider == "mysql")
            {
                i = OleDbHelper.ExecuteNonQuery(ConnectionString, cmdType, strCmd);
            }
            if (Provider == "oracle")
            {
                i = OracleHelper.ExecuteNonQuery(ConnectionString, cmdType, strCmd);
            }
            return i;
        }
        
        public int RunSQLNonQuery(string strCmd, CommandType cmdType, string strConnection)
        {
            int i = 0;
            if (Provider == "mssql")
            {
                i = SqlHelper.ExecuteNonQuery(strConnection, cmdType, strCmd);
            }
            if (Provider == "mysql")
            {
                i = OleDbHelper.ExecuteNonQuery(strConnection, cmdType, strCmd);
            }
            if (Provider == "oracle")
            {
                i = OracleHelper.ExecuteNonQuery(strConnection, cmdType, strCmd);
            }
            return i;
        }

        public object RunSQLScalar(string strCmd, CommandType cmdType)
        {
            object obj = null;
            if (Provider == "mssql")
            {
                obj = SqlHelper.ExecuteScalar(ConnectionString, cmdType, strCmd);
            }
            if (Provider == "mysql")
            {
                obj = OleDbHelper.ExecuteScalar(ConnectionString, cmdType, strCmd);
            }
            if (Provider == "oracle")
            {
                obj = OracleHelper.ExecuteScalar(ConnectionString, cmdType, strCmd);
            }
            return obj;
        }

        public object RunSQLScalar(string strCmd, CommandType cmdType, string strConnection)
        {
            object obj = null;
            if (Provider == "mssql")
            {
                obj = SqlHelper.ExecuteScalar(strConnection, cmdType, strCmd);
            }
            if (Provider == "mysql")
            {
                obj = OleDbHelper.ExecuteScalar(strConnection, cmdType, strCmd);
            }
            if (Provider == "oracle")
            {
                obj = OracleHelper.ExecuteScalar(strConnection, cmdType, strCmd);
            }
            return obj;
        }

        public IDataReader RunSQLReader(string strCmd, CommandType cmdType)
        {
            IDataReader rd = null;
            if (Provider == "mssql")
            {
                rd = SqlHelper.ExecuteReader(ConnectionString, cmdType, strCmd);
            }
            if (Provider == "mysql")
            {
                rd = OleDbHelper.ExecuteReader(ConnectionString, cmdType, strCmd);
            }
            if (Provider == "oracle")
            {
                rd = OracleHelper.ExecuteReader(ConnectionString, cmdType, strCmd);
            }
            return rd;
        }

        public IDataReader RunSQLReader(string strCmd, CommandType cmdType, string strConnection)
        {
            IDataReader rd = null;
            if (Provider == "mssql")
            {
                rd = SqlHelper.ExecuteReader(strConnection, cmdType, strCmd);
            }
            if (Provider == "mysql")
            {
                rd = OleDbHelper.ExecuteReader(strConnection, cmdType, strCmd);
            }
            if (Provider == "oracle")
            {
                rd = OracleHelper.ExecuteReader(strConnection, cmdType, strCmd);
            }
            return rd;
        }

        #endregion

        #region Parameter

        public DataSet RunDynamicSQLQuery(string strStroreCommand, object[] paramValues)
        {
            DataSet ds = new DataSet();
            if (Provider == "mssql")
            {
                ds = SqlHelper.ExecuteDataset(ConnectionString, strStroreCommand, paramValues);
            }
            if (Provider == "mysql")
            {
                ds = OleDbHelper.ExecuteDataset(ConnectionString, strStroreCommand, paramValues);
            }
            if (Provider == "oracle")
            {
                ds = OracleHelper.ExecuteDataset(ConnectionString, strStroreCommand, paramValues);
            }
            return ds;
        }

        public DataSet RunDynamicSQLQuery(string strStroreCommand, object[] paramValues, string strConnection)
        {
            DataSet ds = new DataSet();
            if (Provider == "mssql")
            {
                ds = SqlHelper.ExecuteDataset(strConnection, strStroreCommand, paramValues);
            }
            if (Provider == "mysql")
            {
                ds = OleDbHelper.ExecuteDataset(strConnection, strStroreCommand, paramValues);
            }
            if (Provider == "oracle")
            {
                ds = OracleHelper.ExecuteDataset(strConnection, strStroreCommand, paramValues);
            }
            return ds;
        }

        public int RunDynamicSQLNonQuery(string strStroreCommand, object[] paramValues)
        {
            int i = 0;
            if (Provider == "mssql")
            {
                i = SqlHelper.ExecuteNonQuery(ConnectionString, strStroreCommand, paramValues);
            }
            if (Provider == "mysql")
            {
                i = OleDbHelper.ExecuteNonQuery(ConnectionString, strStroreCommand, paramValues);
            }
            if (Provider == "oracle")
            {
                i = OracleHelper.ExecuteNonQuery(ConnectionString, strStroreCommand, paramValues);
            }
            return i;
        }

        public int RunDynamicSQLNonQuery(string strStroreCommand, object[] paramValues, string strConnection)
        {
            int i = 0;
            if (Provider == "mssql")
            {
                i = SqlHelper.ExecuteNonQuery(strConnection, strStroreCommand, paramValues);
            }
            if (Provider == "mysql")
            {
                i = OleDbHelper.ExecuteNonQuery(strConnection, strStroreCommand, paramValues);
            }
            if (Provider == "oracle")
            {
                i = OracleHelper.ExecuteNonQuery(strConnection, strStroreCommand, paramValues);
            }
            return i;
        }

        public object RunDynamicSQLScalar(string strStoreCommand, object[] paramValues)
        {
            object obj = null;
            if (Provider == "mssql")
            {
                obj = SqlHelper.ExecuteScalar(ConnectionString, strStoreCommand, paramValues);
            }
            if (Provider == "mysql")
            {
                obj = OleDbHelper.ExecuteScalar(ConnectionString, strStoreCommand, paramValues);
            }
            if (Provider == "oracle")
            {
                obj = OracleHelper.ExecuteScalar(ConnectionString, strStoreCommand, paramValues);
            }
            return obj;
        }

        public object RunDynamicSQLScalar(string strStoreCommand, object[] paramValues, string strConnection)
        {
            object obj = null;
            if (Provider == "mssql")
            {
                obj = SqlHelper.ExecuteScalar(strConnection, strStoreCommand, paramValues);
            }
            if (Provider == "mysql")
            {
                obj = OleDbHelper.ExecuteScalar(strConnection, strStoreCommand, paramValues);
            }
            if (Provider == "oracle")
            {
                obj = OracleHelper.ExecuteScalar(strConnection, strStoreCommand, paramValues);
            }
            return obj;
        }

        public IDataReader RunDynamicSQLReader(string strStoreCommand, object[] paramValues)
        {
            IDataReader rd = null;
            if (Provider == "mssql")
            {
                rd = SqlHelper.ExecuteReader(ConnectionString, strStoreCommand, paramValues);
            }
            if (Provider == "mysql")
            {
                rd = OleDbHelper.ExecuteReader(ConnectionString, strStoreCommand, paramValues);
            }
            if (Provider == "oracle")
            {
                rd = OracleHelper.ExecuteReader(ConnectionString, strStoreCommand, paramValues);
            }
            return rd;
        }

        public IDataReader RunDynamicSQLReader(string strStoreCommand, object[] paramValues, string strConnection)
        {
            IDataReader rd = null;
            if (Provider == "mssql")
            {
                rd = SqlHelper.ExecuteReader(strConnection, strStoreCommand, paramValues);
            }
            if (Provider == "mysql")
            {
                rd = OleDbHelper.ExecuteReader(strConnection, strStoreCommand, paramValues);
            }
            if (Provider == "oracle")
            {
                rd = OracleHelper.ExecuteReader(strConnection, strStoreCommand, paramValues);
            }
            return rd;
        }

        #endregion
    }
}